﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Office.PowerPoint.STAMP.TimedText;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using TimedTextLibrary;

namespace Microsoft.Office.PowerPoint.STAMP.Core
{
    /// <summary>
    /// This class facilitates reading CSV files
    /// </summary>
    public static class TTMLReader
    {
        /// <summary>
        /// Converts each line into an array of strings, split by '|'
        /// </summary>
        /// <param name="path">Path of file to read.</param>
        /// <returns>Returns list of string array.</returns>
        public static IEnumerable<TimedTextCaption> ParseTTML(string path)
        {
            IEnumerable<TimedTextCaption> parsedData = new List<TimedTextCaption>();

            try
            {
               // string c = System.IO.Path.GetFullPath(path);
                string sourceText = File.ReadAllText(path);
                parsedData = TimedTextCaptionParser.ParseTTML(sourceText, TimeSpan.Zero, new TimeSpan(0,23,0,0));
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                bool rethrow = ExceptionPolicy.HandleException(ex, "Global Policy");
                if (rethrow)
                    throw;
            }

            return parsedData.Distinct().ToList();;
        }
    }
}
